package com.xrit.sunnyday.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xrit.sunnyday.dao.CheckItemDao;
import com.xrit.sunnyday.entity.QueryPageBean;
import com.xrit.sunnyday.exception.SunnydayException;
import com.xrit.sunnyday.pojo.CheckItem;
import com.xrit.sunnyday.service.CheckItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * 类名称: CheckItemServiceImpl
 * 类描述:TODO
 * 创建人: czb
 * 创建时间:2022/6/8 上午 11:04
 * Version 1.0.0
 */
@Service(interfaceClass = CheckItemService.class)
@Transactional
public class CheckItemServiceImpl implements CheckItemService {
    @Autowired
    private CheckItemDao checkItemDao;
    /*普遍查询*/
    @Override
    public List<CheckItem> findAll() {
        return checkItemDao.findAll();
    }
/*分页查*/
    @Override
    public PageInfo<CheckItem> findPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        if (!StringUtils.isEmpty(queryPageBean.getQueryString())){
            queryPageBean.setQueryString("%" + queryPageBean.getQueryString() + "%");
        }

            List<CheckItem> page = checkItemDao.findPage(queryPageBean.getQueryString());
            return new PageInfo<>(page);

    }
/*添加*/
    @Override
    public void add(CheckItem checkItem) {
        checkItemDao.add(checkItem);
    }
/*根据id删除*/
    @Override
    public void deleteById(Integer id) {
        //先查询有没有
       int count = checkItemDao.findCondition(id);
       if (count>0){
            throw  new SunnydayException("删除的数据已经被绑定");
       }else {
           checkItemDao.deleteById(id);
       }

    }
/*根据id查询*/
    @Override
    public CheckItem findById(Integer id) {
        return checkItemDao.findById(id);
    }
/*修改*/
    @Override
    public void update(CheckItem checkItem) {
        checkItemDao.update(checkItem);
    }
}
